package com.gduf.lsm.service;


import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.ApplyNumStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.Account;
import com.gduf.lsm.entity.ApplyNum;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.ApplyNumMapper;
import com.gduf.lsm.pojo.dto.ApplyStatusDTO;
import com.gduf.lsm.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户数据申请表业务处理
 **/
@Service
public class ApplyNumService {

    @Autowired
    private ApplyNumMapper applyNumMapper;

    /**
     * 新增
     * @param applyNum
     */
    public void add(ApplyNum applyNum) {
        //System.out.println("*******applyNum********"+applyNum);
        //用户id，类型，状态，数据这些必要字段不可为空
        boolean flag=(!applyNum.getApStatus().equals(ApplyNumStatusConstant.APPLY_NUM_STATUS_APPLIED))||applyNum.getUserId()==null||applyNum.getApType()==null||ObjectUtil.isNull(applyNum.getApNum());
        //System.out.println("*******"+flag);
        if(flag){
            throw new CustomException(ResultCodeEnum.APPLY_NUM_NULL_ERROR);
        }
        applyNumMapper.insert(applyNum);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        applyNumMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            applyNumMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param applyNum
     */
    public void updateById(ApplyNum applyNum) {
        applyNumMapper.updateById(applyNum);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public ApplyNum selectById(Integer id) {
        return applyNumMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param applyNum
     * @return
     */
    public List<ApplyNum> selectAll(ApplyNum applyNum) {
        return applyNumMapper.selectAll(applyNum);
    }

    /**
     * 分页查询
     * @param applyNum
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ApplyNum> selectPage(ApplyNum applyNum, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ApplyNum> list = applyNumMapper.selectAll(applyNum);
        return PageInfo.of(list);
    }

    /**
     * 申请员的分页查询
     * @param applyNum
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ApplyNum> selectPageApply(ApplyNum applyNum, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Account account= TokenUtils.getCurrentUser();
        applyNum.setCreateUser(account.getUsername());
        List<ApplyNum> list = applyNumMapper.selectAll(applyNum);
        return PageInfo.of(list);
    }

    /**
     * 修改表状态，因为直接用update的话，含数据部分总会被设置为空，因此改过来
     * @param applyStatusDTO
     */
    public void updateStatus(ApplyStatusDTO applyStatusDTO) {
        applyNumMapper.updateStatus(applyStatusDTO);
    }


}